package com.stage3.zmy.exam4.controller;

import com.github.pagehelper.PageInfo;
import com.stage3.zmy.exam4.bean.Project;
import com.stage3.zmy.exam4.service.ProjectService;
import com.stage3.zmy.exam4.util.PageConstances;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @PROJECT_NAME：stage3_exam4_zhangmengyao
 * @author： zhangyaoyao
 * @date： 2020/1/6 14:36
 * @ 听说你要在海边买一所房子和 你可爱的松子一起住在哪里
 */
@Controller
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @RequestMapping("/getProjectList")
    public String getCourseList(@RequestParam(defaultValue = "1") Integer index, String type, String account, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime ,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime, Model model){


//        System.out.println(index+"****");
//        System.out.println(index+"--index,"+(index-1 )+"---i" );
        if (index == 0 ){
            index = 1;
        }

        PageInfo<Project> projectPage = projectService.getProjects(index, PageConstances.PAGESIZE, type,account,startTime,endTime);
        System.out.println(projectPage.getSize()+"//////");
        for (Project project : projectPage.getList()) {
            System.out.println(project);
        }
//        System.out.println(projectPage.toString());
        model.addAttribute("index",index);
        model.addAttribute("type",type);
        model.addAttribute("account",account);
        model.addAttribute("startTime",startTime);
        model.addAttribute("endTime",endTime);
        model.addAttribute("projectPage",projectPage);

        List<String> types = projectService.getTypes();
        model.addAttribute("types",types);
        return "projectList";
    }


    @RequestMapping("/findProject")
    public String findProject(Integer id ,Model model ){
        Project project = projectService.findProjectById(id);
        model.addAttribute("project",project);
        List<String> types = projectService.getTypes();
        model.addAttribute("types",types);
        return  "updateProject";
    }

    @RequestMapping("/updateProject")
    public String updatehouse(Project  project,Model model){
        int len = projectService.updateProject(project);
        if (len > 0){
            return  "redirect:getProjectList";
        }
        else {
            model.addAttribute("id",project.getId());
            return "findProject";
        }
    }

}
